package com.springboot.demo.web;

import com.github.pagehelper.PageInfo;
import com.springboot.demo.entity.Admin;
import com.springboot.demo.service.AdminService;
import com.springboot.demo.service.BaseSetupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private BaseSetupService baseSetupService;

    @RequestMapping("/regi")
    //@GetMapping("/admin/regi")
    public String toRegister(){
        return "admin_regi";
    }

    @RequestMapping("/index")
    public String toIndex(){
        return "admin_index";
    }

    //注册功能
    @RequestMapping("/register")
    //@GetMapping("/admin/register")
    public String register(String nickname, String telephone, String password,String identifiedPassword,Model model){
        Admin isSuccess = adminService.register(nickname, telephone, password,identifiedPassword);
        if(isSuccess==null) {
            model.addAttribute("msg","Register failed!");
            return "admin_regi";
        }else{
            return "Librarian_login";
        }
    }

    // 这块需要一个拦截器
    @RequestMapping("/tochange")
    public String toChange(){
        return "admin_change";
    }

    // 这块需要一个拦截器
    @RequestMapping("/brarianaccount")
    public String librarianAccount(){
        return "admin_register_account";
    }

    @RequestMapping("/change")
    public String changAccount(String name, String phonenumber, String password, HttpServletRequest httpServletRequest, Model model){
        HttpSession session = httpServletRequest.getSession();
        session.invalidate();
        Admin admin = new Admin(phonenumber, name, password);
        boolean flag = adminService.changeAccount(name,phonenumber,password);
        httpServletRequest.getSession().setAttribute(phonenumber, admin);
        if(flag==false) {
            model.addAttribute("msg","Change failed!");
            return "admin_change";
        }
        return "admin_index";  //登录主页待填
    }

    @RequestMapping("/logout")
    public String logout( HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession(false);
        if(session!=null){
            session.invalidate();
            return "Librarian_login";
        }else{
            return "Librarian_login";
        }
    }


    @ResponseBody
    @RequestMapping("/add")
    public Map<String,Object> addlibrarian(String nickname,String telephone,String password,Model model)
    {
        System.out.println("nickname:"+nickname+",telephone:"+telephone+",password"+password);
        String isSuccess = adminService.regiLibrarian(nickname, telephone, password);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("msg",isSuccess);
        if(isSuccess.equals("success")) {
            resultMap.put("code",1);
            return resultMap;
        }else{
            resultMap.put("code",0);
            model.addAttribute("msg","Register failed!");
        }
        return resultMap;
    }

    @RequestMapping("/basereturn")
    public String basereturn()
    {
        return "admin_return";
    }

    @ResponseBody
    @RequestMapping("/delete")
    public boolean delete(String phone, Model model){
        System.out.println(" ");
        System.out.println("       phone"+phone+"123");
        System.out.println(" ");
        Admin isSuccess = adminService.delete(phone);
        if(isSuccess==null) {
            model.addAttribute("msg","Delete failed!");
            return false;
        }
        return true;
    }

    @ResponseBody
    @RequestMapping("/modify")
    public boolean modify(String phone, String phone1, String lname, String password, Model model){
        System.out.println(phone+" "+phone1+" "+lname+" "+password);
        Admin isSuccess = adminService.modify(phone, phone1, lname, password);
        if(isSuccess==null) {
            model.addAttribute("msg","Modify failed!");
            return false;
        }
        return true;
    }

    @ResponseBody
    @RequestMapping("/batch_delete")
    public boolean batch_delete(String phonelist,Model model)
    {
        System.out.println(phonelist);
        String phone[] = phonelist.split(",");
        String mesg = "something go wrong when deleting ";
        boolean success = true;
        for(String i : phone)
        {
            if(adminService.delete(i)!=null) {
                mesg += "phone=" + i + ",";
                success = false;
            }
        }
        if(success)
        {
            model.addAttribute("msg","batch delete failed!");
            return true;
        }
        return true;
    }

    @GetMapping("/list")
    @ResponseBody
    public Map<String,Object> Search(@RequestParam(required = false,defaultValue = "1")int page,
                                     @RequestParam(required = false,defaultValue = "10")int limit)
    {
//        System.out.println("          what i get is:"+page+" "+limit);
        List<Admin> list = new ArrayList<>();
        list = adminService.getAll(page,limit);
        PageInfo<Admin> pageInfo = new PageInfo(list);
        Map<String,Object> resultMap = new HashMap<>();
//        System.out.println("当前页的查询记录:"+pageInfo.getList().size());
//        System.out.println("当前页码:"+pageInfo.getPageNum());
        resultMap.put("code",0);
        resultMap.put("msg","success");
        resultMap.put("count",pageInfo.getTotal());
        resultMap.put("data",pageInfo.getList());
        return resultMap;
    }

    @PostMapping("/modify_set")
    @ResponseBody
    public boolean Modift_setup(@RequestParam(required = true)int fine,
                             @RequestParam(required = true)int return_time,
                             @RequestParam(required = true)int deposit)
    {
        System.out.println("hahahah");
        adminService.modify_base("fine",fine);
        adminService.modify_base("return_time",return_time);
        adminService.modify_base("deposit",deposit);
        return true;
    }

    @GetMapping("/get_set")
    @ResponseBody
    public Map<String, Integer> Get_setup()
    {
        System.out.println("\n\n\n         fuck         \n\n\n");
        Map<String,Integer> resultMap = new HashMap<>();
        resultMap.put("fine",baseSetupService.getFineValue());
        resultMap.put("return_time",baseSetupService.getRTValue());
        resultMap.put("deposit",baseSetupService.getDepositValue());
        //System.out.println(resultMap);
        return resultMap;
    }


}